package com.edu.controller;


import com.edu.pojo.*;
import com.edu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.*;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.pojo.Activitie;
import com.edu.pojo.Announcement;
import com.edu.pojo.Member;
import com.edu.pojo.users;
import com.edu.service.*;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


@Controller
    @RequestMapping(value="/member")
    public class MemberController {

        @Autowired
        private MemberService memberService;
        @Autowired
        private userservice userservice;

        @Autowired
        private DepartmentService departmentService;


        @Autowired
        private CollegeService collegeService;

        @Autowired
        private AnnouncementService announcementService;

        @Autowired
        private ActivitieService activitieService;
        @Autowired
        private MissionService missionService;
        @Autowired
        private MoneyService moneyService;
        @Autowired
        private  SuggestionService suggestionService;
        @Autowired
        private  EquipmentService equipmentService;

        @RequestMapping(value="/main",method= RequestMethod.GET)
        public String ToMainForm() {
            return "Member/main";
        }

        @RequestMapping(value="/modifyPassword",method=RequestMethod.GET)
        public String ToModifyPassword() {
            return "Member/MemberModifyPassword";
        }


        @RequestMapping(value="/updatemember",method=RequestMethod.GET)
        public String TomemberManage() {
            return "Member/MemberManage";
        }


    @RequestMapping(value="/PathFinish")
    public String FinishPath(Integer id,Model model) {
            model.addAttribute("id",id);
        return "Member/FinishPath";
    }

        @RequestMapping(value="/addsuggestion")
        public String Toaddsuggestion() {
            return "Member/AddSuggestion";
        }

        @RequestMapping(value="/modifySuggestion")
        public String modifySuggestion(HttpServletRequest request,Model model,String studentSuggestions,String memberNu) throws Exception {
            Suggestion su = new Suggestion();
            su.setStudentSuggestions(studentSuggestions);

            su.setMemberNu(memberNu);
            System.out.println(su);

            suggestionService.addSuggestion(su);
            return "redirect:/member/showsuggestion";


        }
        @RequestMapping(value="/showequipment")
        public String showEquipment(Model model,HttpServletResponse response,int department) throws Exception {
            List<Equipment> equipment = equipmentService.showAllEquip(department);
            for(int i=0;i<equipment.size();i++) {
                System.out.println(equipment.get(i));
                int depaId = equipment.get(i).getDepartment();
                String depaNa = departmentService.getNameByID(depaId);
                equipment.get(i).setDepartmentNa(depaNa);
            }
            model.addAttribute("equipmentList", equipment);
            System.out.println("全部成员集合："+equipment);
            return "Member/MemberEqupmentList";
        }

        @RequestMapping(value="/checkActivity",method=RequestMethod.GET)
        public String CheckActivity(Model model) {
            List<Activitie> activitie = activitieService.queryAllActivitie();

            for(int i=0;i<activitie.size();i++) {
                int status = activitie.get(i).getStatus();
                if(status == 1) {
                    activitie.get(i).setStatusName("未审核");
                }else if(status == 2) {
                    activitie.get(i).setStatusName("审核通过");
                }else {
                    activitie.get(i).setStatusName("未通过");
                }
            }

            model.addAttribute("thesisTitleList", activitie);
            System.out.println("查询到该所以的课题有："+activitie);
            return "chair/CheckActivity";
        }

        @RequestMapping(value="/showannouncement")
        public String Announcement(Model model) {
            List<Announcement> list = announcementService.queryAnnouncement();
            SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for(int i=0;i<list.size();i++) {
                Date time4db = list.get(i).getInputTime();
                String formatTime = time.format(time4db);
                list.get(i).setFormatTime(formatTime);
            }
            model.addAttribute("announcementList", list);
            return "Member/Announcement";
        }
        @RequestMapping(value="/showsuggestion")
        public String showsuggestion(Model model) {
            List<Suggestion> list = suggestionService.queryAllSuggestion();

            model.addAttribute("suggestionList", list);
            return "Member/suggestion";
        }
        @RequestMapping(value="/showMember")
        public String ShowMember(Model model,HttpServletResponse response,HttpServletRequest request,String id) throws Exception {
            users user = userservice.queryuserById(id);
            HttpSession session = request.getSession();
            session.setMaxInactiveInterval(3600);
            session.setAttribute("user", user);

            Member member = memberService.getMemberByNO(user.getUserid());
            int depId = member.getDepartment();
            String depName = departmentService.getNameByID(depId);
            member.setDepartmentNa(depName);

            int coID = member.getCollegeId();
            String coName = collegeService.getNameByID(coID+"");
            member.setCollegeNa(coName);

            session.setAttribute("member", member);
            return "Member/MemberManage";
        }


        @RequestMapping(value="/showMyMission")
        public String showmission(Model model,String memberNo) {
            List<Mission> list =missionService.showMyMission(memberNo);
            for(int i=0;i<list.size();i++) {
                int status = (list.get(i).getStatus());
                if(status == 1) {
                    list.get(i).setStatusName("未完成");
                }else if(status == 2) {
                    list.get(i).setStatusName("审核中");
                }else if(status == 3){
                    list.get(i).setStatusName("已完成");
                }
            }
            model.addAttribute("missionList", list);
            return "Member/Mission";
        }
    @RequestMapping(value="/showDepMission")
    public String showDepmission(Model model,String department) {
        List<Mission> list =missionService.showMyMission(department);
        for(int i=0;i<list.size();i++) {
            int status = (list.get(i).getStatus());
            if(status == 1) {
                list.get(i).setStatusName("未完成");
            }else if(status == 2) {
                list.get(i).setStatusName("审核中");
            }else if(status == 3){
                list.get(i).setStatusName("已完成");
            }
        }
        model.addAttribute("missionList", list);
        return "Member/DepMission";
    }
        @RequestMapping(value="/agreeThesis")
        public String agreeThesis(int id,Model model,String memberNo) {

            missionService.agreeThesisTitle(id);
            return "redirect:/member/showMyMission?memberNo="+memberNo;
        }





        @RequestMapping(value="/modifyMember")
        public String adminModifyMember(String id,Model model) {
            Member member = memberService.getMemberByNO(id);

            String memberNo = member.getMemberNo();
            String studentName = member.getStudentName();
            int depId = member.getDepartment();
            String departmentName = departmentService.getNameByID(depId);
            String sex = member.getSex();
            int collegeId =member.getCollegeId();
            String phone = member.getPhone();
            int permssion = member.getPermssion();

            model.addAttribute("memberNo", memberNo);
            model.addAttribute("studentName", studentName);
            model.addAttribute("departmentName", departmentName);
            model.addAttribute("sex", sex);
            model.addAttribute("phone", phone);
            model.addAttribute("collegeId", collegeId);
            model.addAttribute("permssion", permssion);
            return "Member/MemberModify";
        }

        @RequestMapping(value="/modifyMemberToDb")
        public String ModifyMemberToDb(Model model,HttpServletRequest request, String memberNo, String studentName,String sex,String phone,String depart,String collegeId) throws Exception {
            Member member = new Member();
            member.setMemberNo(memberNo);
            member.setStudentName(studentName);
            member.setSex(sex);
            member.setCollegeId(Integer.parseInt(collegeId));
            member.setPhone(phone);
            member.setDepartment(Integer.parseInt(depart));
            member.setPermssion(1);

            int num = memberService.updateStudent(member);

            return "redirect:/member/showMember?id="+memberNo;
        }




        @RequestMapping(value="/MissionDetail",method=RequestMethod.GET)
        public String MissionDetail(Model model,String memberNo,Integer id) {



            Mission mission = missionService.querymissionById(id);

                int status = mission.getStatus();
                if(status == 1) {
                    mission.setStatusName("未接受");
                }else if(status == 2) {
                    mission.setStatusName("已接受");
                }else if(status == 4){
                    mission.setStatusName("已完成");
                }else if(status == 3){
                    mission.setStatusName("审核中");
                }else if(status == 5){
                    mission.setStatusName("未通过");
                }

            model.addAttribute("mission",mission);

            return "Member/MissionDetail";
        }

    @RequestMapping(value="/upload")
    public String upload(HttpServletRequest request, MultipartFile file, Integer id, Model model) throws Exception {

        String localPath = "E:\\Java EE tomcat\\apache-tomcat-9.0.31\\webapps\\filepath\\finishPath\\";
        String exName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = UUID.randomUUID().toString()+exName;
        FileCopyUtils.copy(file.getInputStream(),new FileOutputStream(new File(localPath+fileName)));

        missionService.finishMission(localPath+fileName,id);
        missionService.agreeThesisTitle(id);

        Mission m =missionService.querymissionById(id);
        String memberNo =m.getMemberNo();

        return "redirect:/member/showMyMission?memberNo="+memberNo;


    }




}
